package com.web.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.pojo.vo.CommonResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未登录返回JSON格式数据给前端
 * @Author: wws
 * @Date: 2021/2/2 22:58
 * describe:
*/
@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
        public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
        CommonResult commonResult =CommonResult.UserNotLogin();
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(new ObjectMapper().writeValueAsString(commonResult));
    }
}
